package com.tsx.leetcode.hot100.other;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/remove-element">移除指定的元素</a>
 */
public class Q27RemoveElement {
    public static void main(String[] args) {
        int[] nums = new int[]{0, 1, 2, 2, 3, 0, 4, 2};
        int res = removeElement(nums, 2);
        System.out.println("res = " + res);
        System.out.println("nums = " + Arrays.toString(nums));
    }

    public static int removeElement(int[] nums, int val) {
        int n = nums.length;
        int left = 0, right = 0;
        while (right < n) {
            if (nums[right] != val) {
                nums[left] = nums[right];
                left++;
            }
            right++;
        }
        return left;
    }
}
